<?php
class eBayAuth extends gravy{
	public $token="";
	public $eias="";
	public $user;
	public $userXml;

	function __construct($username=NULL, $sid=NULL, $fullValidate=true){
		parent::__construct();
		$this->validate($username, $sid, $fullValidate);
	}

	static public function isLoggedIn(){
		$gravy=new gravy();
		return($gravy->session("logged_in")==LOGGED_IN_FLAG && strlen($gravy->session("eias"))>0 && strlen($_SESSION['sid'])>0 && strlen($gravy->session("eb@y_un"))>0 && strlen($gravy->session("tkn"))>0 && strlen($gravy->session("firstName"))>0 && strlen($gravy->session("lastName"))>0 && strlen($gravy->session("email"))>0);
	}

	static public function getSigninUrl($params=NULL){
		//$_SESSION["sid"]=$this->getSessionId();
		if(!isset($_SESSION["sid"])||strlen($_SESSION["sid"])<1){
			$_SESSION["sid"]=md5(uniqid(mt_rand(),true));
		}
		$options="";
		if($params!==NULL){
			if(is_string($params)){
				$options="&ruparams=".urlencode($params);
			}
		}
		return(eBayConfig::SIGN_IN_URL.eBayConfig::RUNAME."&sid=".$_SESSION["sid"].$options);
	}

	static public function getSSOUrl($params=NULL){
		//$_SESSION["sid"]=$this->getSessionId();
		if(!isset($_SESSION["sid"])||strlen($_SESSION["sid"])<1){
			$_SESSION["sid"]=md5(uniqid(mt_rand(),true));
		}
		$options="";
		if($params!==NULL){
			if(is_string($params)){
				$options="&ruparams=".urlencode($params);
			}
		}
		return(eBayConfig::SSO_URL.eBayConfig::RUNAME."&sid=".$_SESSION["sid"].$options);
	}

	public function signIn($params=NULL){
		$this->redirect(eBayAuth::getSigninUrl($params));
	}

	public function signOut($params=NULL){
		$this->redirect(eBayConfig::SIGN_OUT_URL);
	}

	public function singleSignOn($params=NULL){
		$this->redirect(eBayAuth::getSSOUrl($params));
	}

	public function validate($username=NULL, $fullValidate=false){
		$result=false;
		if($fullValidate!==TRUE){
			if($result=eBayAuth::isLoggedIn()){
				$this->eias=$this->session("eias");
				$this->token=$this->session("tkn");
				$this->user=array(
					"userName"=>$this->session("eb@y_un"),
					"firstName"=>$this->session("firstName"),
					"lastName"=>$this->session("lastName"),
					"email"=>$this->session("email"),
					"eias"=>$this->session("eias"),
					"token"=>$this->token
				);
			}
		}
		if($username!==NULL && $result===FALSE){
			$obj=new eBayToken($_SESSION['sid'],$username);
			if($this->token=$obj->getToken()){
				$user=new eBayGetUser($username, $this->token);
				if($this->user=$user->get()){
					$this->userXml=$user->xml;
					$this->debug("User",$this->user);
					$this->session("logged_in",LOGGED_IN_FLAG);
					$this->session("eb@y_un",$username);
					$this->session("tkn",$this->token);
					$this->session("firstName",$this->user["firstName"]);
					$this->session("lastName",$this->user["lastName"]);
					$this->session("email",$this->user["email"]);
					$this->session("eias",$this->user["eias"]);
					$this->eias=$this->user["eias"];
					$this->user["token"]=$this->token;
					$this->user["eias"]=$this->eias;
					$result=true;
				}
			}
		}
		return($result);
	}
}
?>